package cn.lili.modules.order.order.callback;

import cn.lili.common.event.TransactionCommitSendMQEvent;
import cn.lili.common.vo.ResponseBean;
import cn.lili.common.vo.ResultMessage;
import cn.lili.exchange.AmqpExchangeProperties;
import cn.lili.modules.goods.client.GoodsClient;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.entity.enums.GoodsMarketEnum;
import cn.lili.modules.goods.entity.vos.yunxin.CallBackVO;
import cn.lili.modules.order.order.entity.dos.MessageTypeFlow;
import cn.lili.modules.order.order.service.MessageTypeFlowService;
import cn.lili.routing.GoodsRoutingKey;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;

/**
 * 上架
 */
@Service("goods.on.sale")
public class GoodsOnSaleService implements MessageType {

    @Autowired
    private GoodsClient goodsClient;

    @Autowired
    private MessageTypeFlowService messageTypeFlowService;

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @Autowired
    private AmqpExchangeProperties amqpExchangeProperties;

    /**
     * 上架
     * @param callBackVO 参数
     * @return 是否成功
     */
    @Override
    public ResponseBean execute(CallBackVO callBackVO) {
        messageTypeFlowService.saveCall(callBackVO);

        Goods goods = new Goods();
        goods.setThirdPartyPlatformId(callBackVO.getProduct_id());
        goods.setMarketEnable(GoodsMarketEnum.UPPER.name());
        boolean b = goodsClient.updateGoods(goods);

        // 发送生成商品索引信息
        applicationEventPublisher.publishEvent(TransactionCommitSendMQEvent.builder().source("生成商品").exchange(amqpExchangeProperties.getGoods()).routingKey(GoodsRoutingKey.GENERATOR_GOODS_INDEX).message(goods.getId()).build());
        return new ResponseBean(0, "操作成功", new HashMap<>());
    }
}
